package javacommon.tag.views;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javacommon.tag.components.MySelect;
import javacommon.util.PropertiesUtil;
import javacommon.util.org.TreeData;
import javacommon.util.org.TreeUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ui.AbstractUITag;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.util.ValueStack;
import com.tech15.core.model.Dept;
import com.tech15.core.service.SearchDataManager;

public class MyDeptTreeSelectTag extends AbstractUITag {
    private String sql;
    private String condition;
    private String dataType;
    protected String listKey;
    protected String listValue;
    protected String headerKey;
    protected String headerValue;
    protected String multiple;
    protected String size;
    protected String isSql;
    protected String sortID;
    
    private SearchDataManager searchDataManager;

	@Override
    public Component getBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
		WebApplicationContext act = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		searchDataManager = (SearchDataManager) act.getBean("searchDataManager");
		
        return new MySelect(stack, request, response);
    }

    @Override
    protected void populateParams(){
        super.populateParams();
        if(PropertiesUtil.pps == null){
        	PropertiesUtil.pps = new Properties();
    		try{
    			String path = (getClass().getClassLoader().getResource("").toURI()).getPath();
    			FileInputStream fis = new FileInputStream(path + "config.properties");
    			PropertiesUtil.pps.load(fis);
    		}catch (Exception e){
    			e.printStackTrace();
    		}
        }
        String s = PropertiesUtil.pps.getProperty(sql);
        MySelect mySelect = (MySelect)component;
        List<Object> list = searchDataManager.searchData(s,isSql,condition,dataType);
        List<Dept> newList = new ArrayList<Dept>();
        if(list != null && list.size()>0){
        	for(int i=0;i<list.size();i++){
        		Dept d = (Dept) list.get(i);
        		List<Dept> dl = TreeUtils.setDeptTreeList(d);
    			newList.addAll(dl);
        	}
        }
    	mySelect.addParameter("list", newList);
        mySelect.setHeaderKey(headerKey);
        mySelect.setHeaderValue(headerValue);
        mySelect.setListKey(listKey);
        mySelect.setListValue(listValue);
        mySelect.setMultiple(multiple);
        mySelect.setSize(size);
        mySelect.setCondition(condition);
        mySelect.setIsSql(isSql);
        mySelect.setSortID(sortID);
    }

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}

	public void setHeaderValue(String headerValue) {
		this.headerValue = headerValue;
	}

	public void setListKey(String listKey) {
		this.listKey = listKey;
	}

	public void setListValue(String listValue) {
		this.listValue = listValue;
	}
	
	public void setMultiple(String multiple) {
        this.multiple = multiple;
    }

    public void setSize(String size) {
        this.size = size;
    }

	public void setIsSql(String isSql) {
		this.isSql = isSql;
	}

	public void setSortID(String sortID) {
		this.sortID = sortID;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

}